package com.example.day002_kt_livedata;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

public class MyView extends View {
    private float progress=0.0f;
    public int index = 0;

    Paint paint1 = new Paint();//绘制圆弧
    Paint paint2 = new Paint();//绘制文字

    public MyView(Context context) {
        super(context);
    }

    public MyView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        paint1.setAntiAlias(true);
        paint1.setColor(Color.BLUE);
        paint1.setStyle(Paint.Style.STROKE);//空心
        paint1.setStrokeWidth(10);


        paint2.setAntiAlias(true);
        paint2.setStyle(Paint.Style.FILL);//字体必须实心
        paint2.setTextAlign(Paint.Align.CENTER);//居中
        paint2.setTextSize(80);


        RectF rectF = new RectF(200, 200, 600, 600);
        canvas.drawArc(rectF,270,progress,false,paint1);

        canvas.drawText(index+"%",rectF.centerX(),rectF.centerY(),paint2);



    }

    //连续更新  需要在子线程中
    public void start(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 300; i++) {
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    progress+=1.2f;
                    MyView.this.postInvalidate();
                    if (i%3==0){
                        index+=1;
                    }
                }
            }
        }).start();

    }
}
